<?php
 class SurveyController extends Controller{
  public $isActivated = 0;
          public function accessRules()
		 {
			return array(
			   array(
					'allow',  // allow all users to access 'index' and 'view' actions.
					'expression'=>'$user->rights>=2',
				),
				array(
					'deny',  // deny all users
					'users'=>array('*'),
					'message'=>'You do not have access to this page ! ! ',
				),
				
			);
		 } 
     public function actionIndex(){
		 $this->breadcrumbs=array(
               '调查问卷'=>array('index'),
               '查看问卷统计',
             );
		 $surs = Survey::model()->findAll('ispost = 1');
		 $this->render("index",array('surs'=>$surs));
		 
	 }
	 public  function actionSurveyprofile(){
		 $this->breadcrumbs=array(
               '调查问卷'=>array('index'),
               '查看问卷统计结果',
             );
		 $request = Yii::app()->request;
		 $sur = Survey::model()->findByPk($request->getParam('surveyId'));
		 if($sur){
			 
			 
			 
		 }else{
			 
			 $this->redirect(array('survey/index'));
		 }
		 $this->render("surveyprofile",array('sur'=>$sur));
		 
	 }
	 public  function actionAddsurvey(){
		  $this->breadcrumbs=array(
               '调查问卷'=>array('index'),
               '问题添加',
             );
		 $request = Yii::app()->request;
		 $sur = Survey::model()->findByPk($request->getParam('id'));
		 if($sur){
			 if($sur->ispost == '1'){
				  Yii::app()->user->setFlash('add','已发布，不能修改');
				  $this->redirect(array('survey/newsurvey'));
				 
			 }
			$qus = Question::model()->findAll('surveyId = :s',array(':s'=>$sur->surveyId));
			 
		 }else{
			 $this->redirect(array('survey/newsurvey'));
			 
		 }
		 
		 $this->render("addsurvey",array('sur'=>$sur,'qus'=>$qus));
		 
	 }
	 public function actionAddquestion(){
		 $request = Yii::app()->request;
		 if($request->requestType == 'POST'){
			 $sur = Survey::model()->findByPk($request->getParam('surveyId'));
			 if($sur){
				 $content = array();
				 $statistic = array();
				 
				 $q = new Question;
				
				 if($request->getParam('type') == 'tiankong'){
				        $content = array();
						$statistic = array();
				 
				 }else if($request->getParam('type') == 'danxuan' || $request->getParam('type') == 'duoxuan'){
					 $content = $request->getParam('d');
					 foreach($content as $key => $value){
						 $statistic[$key] = 0;
					 }
					 
				 }
				 $q->surveyId = $sur->surveyId;
				 $q->type = $request->getParam('type');
				 $q->name = $request->getParam('name');
				 $q->content = json_encode($content);
				 $q->addtime = date('Y-m-d H:i:s');
				 $q->statistic = json_encode($statistic);
				 
				 $sur->questionnum += 1;
				 
				 if($q->save()){
					if($sur->save()){ 
						Yii::app()->user->setFlash('addquestion','试题添加成功，请到 预览试卷 查看');
					}else{
						$this->logAr($sur);
						Yii::app()->user->setFlash('addquestion','试题添加成功，请到 预览试卷 查看。试卷题目数量新增失败');
					}
				 }else{
					 $this->logAr($q);
					 Yii::app()->user->setFlash('addquestion','添加失败');
				 }
				 
			 }else{
				 
				  $this->redirect(array('survey/newsurvey'));
			 }
			 
		 }else{
			 $this->redirect(array('survey/newsurvey'));
			 
		 }
		 $this->redirect(array('addsurvey','id'=>$sur->surveyId));
		 
	 }
	 public function actionPostsurvey(){
		 $request = Yii::app()->request;
		 $sur = Survey::model()->findByPk($request->getParam('surveyId'));
		 if($sur){
			 $sur->ispost = 1;
			 if($sur->save()){
				 Yii::app()->user->setFlash('post','发布成功，现在学生可以答题（发布之后，无法再次修改试卷）。');
			 }else{
				 $this->logAr($sur);
				 Yii::app()->user->setFlash('post','发布失败');
			 }
			 
		 }else{
			 
			$this->redirect(array('survey/newsurvey')); 
		 }
		 
		$this->redirect(array('survey/newsurvey')); 
		 
	 }
	 public  function actionNewsurvey(){
		 
		 $this->breadcrumbs=array(
               '调查问卷'=>array('index'),
               '所有调查问卷',
             );
		 $request = Yii::app()->request;
		 if($request->requestType == 'POST'){
			 
			 $this->dealNewSurvey();
			 
		 }else{
			  $ci = new CDbCriteria;
			  $total = Survey::model()->count($ci);
			  $pages =  new CPagination($total);
			  $pages->pageSize = 20;
			  $pages->applyLimit($ci);
			  $surs = Survey::model()->findAll($ci);
			  
		 }
		
		 
		 $this->render("newsurvey",array('surs'=>$surs,'pages'=>$pages));
		 
	 }
	 
	 protected  function dealNewSurvey(){
         $request = Yii::app()->request;		
	 	 $sur = new Survey;
		 $sur->name = $request->getParam('name');
		 $sur->detail = $request->getParam('detail');
		 $sur->attendnum = 0;
		 $sur->questionnum  = 0;
		 $sur->addtime = date('Y-m-d H:i:s');
		 $sur->ispost = 0;
		 if($sur->save()){
			$this->redirect(array('survey/addsurvey','id'=>$sur->surveyId));
		 }else{
			 Yii::app()->user->setFlash('add','添加失败');
			 $this->redirect(array('survey/newsurvey'));
			 
		 }
	 }
	 
	 public function actionDelete(){
		 $request = Yii::app()->request;
		 if($request->isAjaxRequest){
			 $sur = Survey::model()->findByPk($request->getParam('surveyId'));
			 if($sur){
				 
				 
				 if($sur->delete()){
					  echo json_encode(array('msg'=>"删除成功","code"=>200));
					 
				 }else{
					 $this->logAr($sur);
					 echo json_encode(array('msg'=>"删除失败","code"=>501));
				 }
				 
			 }else{
				 echo json_encode(array('msg'=>"没有找到这个条目","code"=>404));
			 }
			 
		 }else{
			 
			  $this->redirect(array('survey/newsurvey'));
		 }
		 
	 }
}
?>